namespace ResponseLocalLibrary
{
    using BEGDC.CC.BL.Common;
    using BEGDC.CC.BL.Common.Query;
    using BEGDC.CC.BL.UserManagement;
    using BEGDC.CC.BL.Workflow;
    using BEGDC.CC;
    using BEGDC.Utility.Castle;
    using IAM.Interface.Model;
    using Nullables;
    using System;
    using System.Collections;
    using System.Data;
    using System.Text;
    using ASBCC.Database.EntityFramework;
    using System.Linq;
    using System.Collections.Generic;
    using ASB.CC.Business;
    using ASBCC.Business;

    public class CCSFacade
    {
        public static void DeleteDelegationInfo(string empUPI, string roleID)
        {
            //IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            //BEGDC.CC.Entity.UserRole userRoleByUserAndRoleID = user.GetUserRoleByUserAndRoleID(empUPI, roleID);
            //if (userRoleByUserAndRoleID.DelegateEndDate != 0)
            //{
            //    CCPrincipal.RemovePrincipal(userRoleByUserAndRoleID.DelegateUser.NameEN);
            //    userRoleByUserAndRoleID.DelegateUser = null;
            //    userRoleByUserAndRoleID.DelegateStartDate = NullableDateTime.Default;
            //    userRoleByUserAndRoleID.DelegateEndDate = NullableDateTime.Default;
            //    userRoleByUserAndRoleID.IsEnabled = false;
            //    user.SaveUserRole(userRoleByUserAndRoleID);
            //}
        }

        public static void DetachAllDelegationFromUser(string empUPI)
        {
            (AppContainer.Instance["CC.BL.User"] as IUser).DetachAllDelegationFromUser(empUPI);
        }

        public static void DetachOneDelegationFromUser(string empUPI,string roleID)
        {
            (AppContainer.Instance["CC.BL.User"] as IUser).DetachOneDelegationFromUser(empUPI,roleID);
        }

        public static void DetachRoleFromUser(string employeeUPI)
        {
            (AppContainer.Instance["CC.BL.User"] as IUser).DetachRoleFromUser(employeeUPI);
        }

        public static void DisableIdentity(string employeeUPI)
        {
            IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            //Employee userByID = user.GetUserByID(employeeUPI);
            //userByID.IsActived = false;
            //user.SaveEmployee(userByID);
        }

        public static void EnableIdentity(string employeeUPI)
        {
            //IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            //Employee userByID = user.GetUserByID(employeeUPI);
            //userByID.IsActived = true;
            //user.SaveEmployee(userByID);
        }

        public static string GenerateAllUserACI()
        {
            //IList userList = (AppContainer.Instance["CC.BL.User"] as IUser).GetUserList();
            var userList = IAMHelper.GetAllUser();
            UserACIs is2 = new UserACIs();
            foreach (var obj2 in userList)
            {
                UserACI singleUserACI = GetSingleUserACI(obj2);
                if (singleUserACI != null)
                {
                    is2.Items.Add(singleUserACI);
                }
            }
            string xML = is2.XML;
            string str2 = "<ExportDate ExportTime=\"\" AppName=\"CUSTOMER_CARE\" DataType=\"IDEN_USERLIST\" ><Result ErrorCode=\"0\" ErrorMessage=\"OK\" > </Result>";
            string str3 = "</ExportDate>";
            return (str2 + xML + str3);
        }

        public static string GenerateLogFile()
        {
            IUtility utility = AppContainer.Instance["CC.BL.Utility"] as IUtility;
            string start = DateTime.Today.AddDays(-60.0).ToString("M/d/yyyy");
            string end = start;
            DataTable loginoutLog = utility.GetLoginoutLog(start, end);
            StringBuilder builder = new StringBuilder("<ExportData ExportTime=\"\" AppName=\"\" DataType=\"AUDIT_FETCH\"><Result ErrorCode=\"0\" ErrorMessage=\"OK\" />");
            if (loginoutLog != null)
            {
                builder.Append("<Logs Count=\" " + loginoutLog.Rows.Count + "\"> ");
                if (loginoutLog.Rows.Count != 0)
                {
                    for (int i = 0; i < loginoutLog.Rows.Count; i++)
                    {
                        builder.Append("<log SerialNo=\"" + i.ToString() + "\" Time=\"" + loginoutLog.Rows[i]["Time"].ToString() + "\" Type=\"Information\" Operation=\"Log\" AppAccount=\"" + loginoutLog.Rows[i]["EmployeeNo"].ToString() + "\" IPAddr=\"127.0.0.1\" Note=\"" + loginoutLog.Rows[i]["OperatorName"].ToString() + "  " + loginoutLog.Rows[i]["Description"].ToString() + " \" /> ");
                    }
                }
            }
            builder.Append("</Logs>");
            builder.Append("</ExportData>");
            return builder.ToString();
        }

        public static string GetCatByUserRole(string employeeUPI, string roleName)
        {
            //BEGDC.CC.Entity.UserRole userRoleByUserAndRole = (AppContainer.Instance["CC.BL.User"] as IUser).GetUserRoleByUserAndRole(employeeUPI, roleName);
            //if (userRoleByUserAndRole.CATBranch == null)
            //{
            //    return string.Empty;
            //}
            //return userRoleByUserAndRole.CATBranch.Id.ToString();
            return "";
        }

        public static DataTable GetCatForDirector()
        {
            IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            IUtility utility = AppContainer.Instance["CC.BL.Utility"] as IUtility;
            return user.GetDepartmentByType("CAT");
        }

        public static IList GetCostCenterForCCHead()
        {
            IUtility utility = AppContainer.Instance["CC.BL.Utility"] as IUtility;
            return utility.BuildDicList("CostCenter-OCO");
        }

        public static DataTable GetCostCenterHeadByUserRole(string employeeUPI, string roleName)
        {
            IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            //BEGDC.CC.Entity.UserRole userRoleByUserAndRole = user.GetUserRoleByUserAndRole(employeeUPI, roleName);
            //return user.GetCostCenterHeadByUserRoleID(userRoleByUserAndRole.Id);
            return null;
        }

        public static DataTable GetDelegationInfoByUser(string empUPI)
        {
            IQuery query = AppContainer.Instance["CC.BL.Query"] as IQuery;
            return query.QueryDelegationRoleList(empUPI);
        }

        public static DataTable GetDeptForFNC()
        {
            IQuery query = AppContainer.Instance["CC.BL.Query"] as IQuery;
            return query.GetAllDepartment();
        }

        public static DataTable GetFNCDeptByUserRole(string employeeUPI, string roleName)
        {
            IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            //BEGDC.CC.Entity.UserRole userRoleByUserAndRole = user.GetUserRoleByUserAndRole(employeeUPI, roleName);
            //return user.GetFNCDeptByUserRoleID(userRoleByUserAndRole.Id);
            return null;
        }

        public static IList GetRoleByUserID(string employeeUPI)
        {
            IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            return user.GetRolesByUserID(employeeUPI);
        }

        public static DataTable GetRoleList()
        {
            IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            return user.GetDelegationRoleDataTable();
        }

        public static UserACI GetSingleUserACI(string upi)
        {
            UserACI raci = new UserACI();
            raci.AccountInfo = new AccountInfo();
            raci.AccountInfo.HRISEID = string.Empty;
            raci.AccountInfo.ADAccount = string.Empty;
            raci.AccountInfo.CSLAccount = string.Empty;
            raci.AccountInfo.CILX500 = string.Empty;
            raci.AccountInfo.UPI = upi;
            raci.AccountInfo.AppAccount = upi;
            raci.ACISessions = new ACISessions();
            try
            {
                List<ASBCC.Database.EntityFramework.Role> roles = IAMHelper.GetRoleByUserID(upi);
                if (roles == null)
                {
                    return null;
                }
                if (roles.Count == 0)
                {
                    return null;
                }
                foreach (var role in roles)
                {
                    ACISession session = new ACISession();
                    CheckedItem item = new CheckedItem();
                    item.ACItemID = role.RoleID.ToString();
                    item.Attr1 = "role";
                    item.Attr2 = role.BelongsTo;
                    item.Attr3 = role.Name;
                    item.Value = true;
                    session.Items.Add(item);
                    if (role.Name.ToLower() == "costcenterhead")
                    {
                        DataTable costCenterUserRole = IAMHelper.GetCostCenterByUserCIL(upi);
                        for (int i = 0; i < costCenterUserRole.Rows.Count; i++)
                        {
                            CheckedItem item2 = new CheckedItem();
                            item2.ACItemID = costCenterUserRole.Rows[i]["CostCenterId"].ToString();
                            item2.Value = true;
                            item2.Attr1 = "costcenterhead";
                            item2.Attr2 = costCenterUserRole.Rows[i]["CostCenterCode"].ToString();
                            session.Items.Add(item2);

                        }
                    }
                    if (role.Name.ToLower() == "cio user")
                    {
                        foreach (var role1 in IAMHelper.GetCountryByUserID(upi))
                        {
                            CheckedItem item3 = new CheckedItem();
                            item3.ACItemID = role1.RoleID.ToString();
                            item3.Value = true;
                            item3.Attr1 = "country";
                            item3.Attr2 = role1.Name;
                            session.Items.Add(item3);
                        }
                    }
                    raci.ACISessions.Items.Add(session);
                }
                ACISession session1 = new ACISession();
                bool sessionFlag = false;
                foreach (string roleDetail in RoleAssignmentHelper.GetUserRole(RoleAssignmentHelper.GetCILByEmployeeNo(upi)).Split(','))
                {
                    string[] roleDetails = roleDetail.Split(';');
                    if (roleDetails == null || roleDetails.Count() == 0)
                        continue;
                    CheckedItem item4 = new CheckedItem();
                    item4.Value = true;
                    item4.Attr1 = "CIDRole";
                    if (roleDetails[0] == "Cluster")
                    {
                        item4.ACItemID = "Cluster";
                        item4.Attr4 = roleDetails[1];
                        session1.Items.Add(item4);
                        sessionFlag = true;
                        continue;
                    }
                    if (roleDetails[0] == "Catvp")
                    {
                        item4.ACItemID = "Catvp";
                        item4.Attr3 = roleDetails[1];
                        session1.Items.Add(item4);
                        sessionFlag = true;
                        continue;
                    }
                    if (roleDetails[0] == "CommercialP")
                    {
                        item4.ACItemID = "CommercialP";
                        item4.Attr2 = roleDetails[1];
                        session1.Items.Add(item4);
                        sessionFlag = true;
                        continue;
                    }
                    if (roleDetails[0] == "CommercialC")
                    {
                        item4.ACItemID = "CommercialC";
                        item4.Attr3 = roleDetails[1];
                        session1.Items.Add(item4);
                        sessionFlag = true;
                        continue;
                    }
                    if (roleDetails[0] == "DPSO")
                    {
                        item4.ACItemID = "DPSO";
                        item4.Attr2 = roleDetails[1];
                        session1.Items.Add(item4);
                        sessionFlag = true;
                        continue;
                    }
                    if (roleDetails[0] == "PSO")
                    {
                        item4.ACItemID = "PSO";
                        item4.Attr2 = roleDetails[1];
                        session1.Items.Add(item4);
                        sessionFlag = true;
                        continue;
                    }
                    if (roleDetails[0] == "Sales")
                    {
                        item4.ACItemID = "Sales";
                        item4.Attr2 = roleDetails[1];
                        item4.Attr3 = roleDetails[2];
                        session1.Items.Add(item4);
                        sessionFlag = true;
                        continue;
                    }
                }
                if (sessionFlag)
                    raci.ACISessions.Items.Add(session1);
            }
            catch (Exception)
            {
                return null;
            }
            return raci;
        }

        public static bool SaveCostCenterHead(string employeeUPI, string roleID, string costcenterID)
        {
            //IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            //BEGDC.CC.Entity.UserRole userRoleByUserAndRoleID = user.GetUserRoleByUserAndRoleID(employeeUPI, roleID);
            //return user.SaveCostCenterHead(userRoleByUserAndRoleID.Id, Convert.ToInt32(costcenterID.Replace("c_", string.Empty)));
            return true;
        }

        public static bool SaveFNCDept(string employeeUPI, string roleID, string deptID)
        {
            //IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            //BEGDC.CC.Entity.UserRole userRoleByUserAndRoleID = user.GetUserRoleByUserAndRoleID(employeeUPI, roleID);
            //return user.SaveFNCDept(userRoleByUserAndRoleID.Id, Convert.ToInt32(deptID.Replace("f_", string.Empty)));
            return true;
        }

        public static bool SaveUserRole(string employeeUPI, string roleList)
        {
            IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            return user.SaveUserRole(employeeUPI, "-1", roleList);
        }

        public static bool SaveUserRole_CAT(string employeeUPI, string roleID, string catID)
        {
            //IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            //BEGDC.CC.Entity.UserRole userRoleByUserAndRoleID = user.GetUserRoleByUserAndRoleID(employeeUPI, roleID);
            //return user.SaveUserRole_CAT(userRoleByUserAndRoleID.Id, Convert.ToInt32(catID.Replace("a_", string.Empty)));
            return true;
        }

        public static void SetDelegationInfo(string empUPI, string delUPI, string roleID, string beginDate, string endDate)
        {
            IUser user = AppContainer.Instance["CC.BL.User"] as IUser;
            BEGDC.CC.Entity.UserRole userRoleByUserAndRoleID = user.GetUserRoleByUserAndRoleID(empUPI, roleID);
            userRoleByUserAndRoleID.IsEnabled = true;
            BEGDC.CC.Entity.Employee employee = new BEGDC.CC.Entity.Employee();
            employee.Id = delUPI;
            userRoleByUserAndRoleID.DelegateUser = employee;
            userRoleByUserAndRoleID.DelegateStartDate = NullableDateTime.Parse(beginDate);
            userRoleByUserAndRoleID.DelegateEndDate = NullableDateTime.Parse(endDate);
            user.SaveUserRole(userRoleByUserAndRoleID);
        }

        public static bool ValidateIdentity(string employeeUPI)
        {
            using (var dc = new CCEntities())
            {
                if (dc.Employees.Where(p => p.EmployeeNo == employeeUPI && p.IsEnabled == true).Count() > 0)
                    return true;
                return true;
            }

        }
    }
}

